<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>3天气案例-立即监视</title>
        <!-- 引入Vue -->
        <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
        <!-- 准备好一个容器-->
        <div id="demo">
            <h1>今天天气很{{info}}</h1>
            <button @click='changeWeather'>点击切换天气</button>
            <img :src="url" alt="">
        </div>
    </body>

    <script type="text/javascript">
        //阻止 vue 在启动时生成生产提示。
        Vue.config.productionTip = false
        new Vue({
            el:'#demo',
            data:{
                isHot:true,
                url:'https://s3.ax1x.com/2020/12/13/reC1IA.jpg'
            },
            computed: {
                info(){
                    return this.isHot?'炎热':'凉爽'
                }
            },
            methods: {
               changeWeather(){
                this.isHot=!this.isHot
               }
            },
            watch: {
                // isHot:{
                //     immediate:true,
                //     handler(newValue,oldValue){
                //         console.log(newValue,oldValue)
                //         if(newValue===true){
                //             this.url='https://s3.ax1x.com/2020/12/13/reC1IA.jpg'
                //         }else{
                //             this.url='https://s3.ax1x.com/2020/12/13/reCaqg.jpg'
                //         }
                //     }
                // }

                //简写方法,但是简写之后不能增加配置项,如立即监视和深度监视(需要写在对象里面)
                isHot(newValue,oldValue){                                             
                        console.log(newValue,oldValue)
                        if(newValue===true){
                            this.url='https://s3.ax1x.com/2020/12/13/reC1IA.jpg'
                        }else{
                            this.url='https://s3.ax1x.com/2020/12/13/reCaqg.jpg'
                        }
                    }
                
            }
        })
    </script>
</html>